package com.cn.sl.server.util

/**
 * 查询树形结构集合中指定节点的子节点
 */
fun <TreeNode, ID> findChildren(
    parent: TreeNode,
    treeCollection: Collection<TreeNode>,
    getId: (TreeNode) -> ID,
    getParentId: (TreeNode) -> ID
): Collection<TreeNode> {
    return treeCollection.filter { getId(parent) == getParentId(it) }
}

/**
 * 查询树形结构集合中指定节点的子节点及子节点的子节点
 */
fun <TreeNode, ID> findDeepChildren(
    parent: TreeNode,
    treeCollection: Collection<TreeNode>,
    getId: (TreeNode) -> ID,
    getParentId: (TreeNode) -> ID
): Collection<TreeNode> {
    val result = arrayListOf<TreeNode>()

    val children = treeCollection.filter { getId(parent) == getParentId(it) }
    result.addAll(children)
    children.forEach { child ->
        result.addAll(findChildren(child, treeCollection, getId, getParentId))
    }
    return result
}